﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddingABootImage
{
  class Program
  {

    // This script burns a boot image and data files to disc in a 
    // single session  using files from a single directory tree. 
    //Copyright (C) Microsoft Corp. 2006

    // *** CD/DVD disc file system types
    public const int FsiFileSystemISO9660 = 1;
    public const int FsiFileSystemJoliet = 2;
    public const int FsiFileSystemUDF = 4;

    static void Main(string[] args)
    {

      dynamic index;                // Index to recording drive.
      dynamic recorder;             // Recorder object
      dynamic path;                 // Directory of files to burn
      dynamic stream;               // Data stream for burning device
      dynamic bootFile;             // Path and filename of boot image

      index = 1;                // Second drive on the system
      path = @"g:\BurnDir";
      bootFile = @"g:\BootImg\etfsboot.com";

      // Create a DiscMaster2 object to connect to CD/DVD drives.
      dynamic g_DiscMaster;
      g_DiscMaster = new IMAPI2.MsftDiscMaster2();

      // Create a DiscRecorder object for the specified burning device.
      dynamic uniqueId;
      recorder = new IMAPI2.MsftDiscRecorder2();
      uniqueId = g_DiscMaster.Item(index);
      recorder.InitializeDiscRecorder(uniqueId);

      // -------- Adding Boot Image Code -----
      dynamic bootOptions;
      Console.WriteLine("Creating BootOptions");
      bootOptions = new IMAPI2FS.BootOptions();
      bootOptions.Manufacturer = "Microsoft";
      bootOptions.PlatformId = 0;       // x86 processor
      bootOptions.Emulation = 0;       // EmulationType.EmulationNone

      // Need a stream for the boot image file 
      const int adFileTypeBinary = 1;
      dynamic bootStream;
      bootStream = new ADODB.Stream();
      Console.WriteLine("Creating IStream for file " + bootFile);
      bootStream.Open();
      bootStream.Type = adFileTypeBinary;
      bootStream.LoadFromFile(bootFile);
      bootOptions.AssignBootImage(bootStream);

      // Create disc file system image (ISO9660 in this example)
      dynamic FSI;
      FSI = new IMAPI2FS.MsftFileSystemImage();
      FSI.ChooseImageDefaults(recorder);

      // Add the boot directory and its contents to the file system 
      FSI.BootImageOptions = bootOptions;

      // Add the content directory and files to the file system 
      FSI.root.AddTree(path, false);

      dynamic result;
      result = FSI.CreateResultImage();
      stream = result.ImageStream;

      // Create and write stream to disc using the specified recorder.
      dynamic dataWriter;
      dataWriter = new IMAPI2.MsftDiscFormat2Data();
      dataWriter.Recorder = recorder;
      dataWriter.ClientName = "IMAPIv2 TEST";

      dataWriter.write(stream);
      Console.WriteLine("----- Finished writing content -----");

    }
  }
}